package com.yyyyglxt.yygl.workdayManage.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yyyyglxt.common.controller.BaseController;
import com.yyyyglxt.yygl.workdayManage.domain.WorkApplyDO;
import com.yyyyglxt.yygl.workdayManage.domain.WorkdayDO;
import com.yyyyglxt.yygl.workdayManage.service.WorkApplyService;
import com.yyyyglxt.yygl.workdayManage.service.WorkdayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yyyyglxt.common.utils.PageUtils;
import com.yyyyglxt.common.utils.Query;
import com.yyyyglxt.common.utils.R;

/**
 *
 * @author genhaoer
 * @email 1024@ac.com
 * @date 2022-04-30 21:15:08
 */
 
@Controller
@RequestMapping("/yygl/workApply")
public class WorkApplyController extends BaseController {
	private static final String prefix = "/yygl/workApply";
	@Autowired
	private WorkApplyService workApplyService;

	@Autowired
	private WorkdayService workdayService;
	
	@GetMapping()
	String WorkApply(){
	    return prefix+"/workApply";
	}

	@GetMapping("/listByDocPage")
	String listByDocPage(Model model){
		model.addAttribute("did",getUserId());
		return prefix+"/listByDocPage";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<WorkApplyDO> workApplyList = workApplyService.list(query);
		int total = workApplyService.count(query);
		PageUtils pageUtils = new PageUtils(workApplyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(@RequestParam Map<String, Object> params,Model model){
		model.addAttribute("did",getUserId());
		model.addAttribute("dname",getUsername());
		model.addAttribute("wid",params.get("wid"));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		model.addAttribute("applytime",sdf.format(new Date()));
		model.addAttribute("state","等待处理");
	    return prefix+"/add";
	}

	@GetMapping("/edit/{aid}")
	String edit(@PathVariable("aid") Integer aid,Model model){
		WorkApplyDO workApply = workApplyService.get(aid);
		model.addAttribute("workApply", workApply);
	    return prefix+"/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( WorkApplyDO workApply){
		if(workApplyService.save(workApply)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( WorkApplyDO workApply){
		if(workApply.getState().equals("同意")){
			WorkdayDO workdayDO = new WorkdayDO();
			workdayDO.setWid(workApply.getWid());
			if(workApply.getRequest().equals("申请停诊")){
				workdayDO.setState("停诊");
			}
			else if(workApply.getRequest().equals("申请出诊")){
				workdayDO.setState("工作");
			}
			workdayService.update(workdayDO);
		}
		workApplyService.update(workApply);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer aid){
		if(workApplyService.remove(aid)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] aids){
		workApplyService.batchRemove(aids);
		return R.ok();
	}
	
}
